package com.movecar.adapter;

import java.util.ArrayList;

import android.app.Activity;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.movecar.R;
import com.movecar.model.Comments;
import com.movecar.utils.AppConfig;
import com.movecar.utils.imageloader.Constants;
import com.nostra13.universalimageloader.core.ImageLoader;

public class GroupDiscussListAdapter extends BaseAdapter {

	private LayoutInflater inflater;
	private ViewHolder viewHolder = null;
	private ArrayList<Comments> commentsData;
	@SuppressWarnings("unused")
	private Activity activity;
	private ImageLoader imageLoader;

	public GroupDiscussListAdapter(Activity activity, ArrayList<Comments> resultList) {
		this.commentsData = resultList;
		this.activity = activity;
		inflater = LayoutInflater.from(activity);
		imageLoader = ImageLoader.getInstance();
		imageLoader.init(Constants.imageLoaderConfiguration(activity));
	}

	@Override
	public int getCount() {

		return commentsData.size();
	}

	@Override
	public Object getItem(int arg0) {

		return commentsData.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {

		return arg0;
	}

	@Override
	public View getView(final int position, View view, ViewGroup arg2) {

		if (view == null) {
			viewHolder = new ViewHolder();
			view = inflater.inflate(R.layout.group_discuss_item, null);

			viewHolder.userNickName = (TextView) view.findViewById(R.id.userName);
			viewHolder.content = (TextView) view.findViewById(R.id.content);
			viewHolder.createDate = (TextView) view.findViewById(R.id.createDate);
			viewHolder.user_icon = (ImageView) view.findViewById(R.id.userImg);
			view.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) view.getTag();
		}
		if ("1".equals(commentsData.get(position).getMmFlag())) {

			String nickName = commentsData.get(position).getNickname().toString();
			viewHolder.userNickName.setTextColor(Color.RED);
			viewHolder.userNickName.setText(nickName);

		} else {
			String nickName = commentsData.get(position).getNickname().toString();
			viewHolder.userNickName.setText(nickName);
		}

		viewHolder.content.setText(commentsData.get(position).getCommentContent());
		viewHolder.createDate.setText(commentsData.get(position).getCommentTime());
		if (commentsData.get(position).getIcon().contains("default")) {
			imageLoader.displayImage(AppConfig.HOST + commentsData.get(position).getIcon(), viewHolder.user_icon,
					Constants.image_display_options);
		} else {
			imageLoader.displayImage(AppConfig.HOST + commentsData.get(position).getIcon(), viewHolder.user_icon,
					Constants.image_display_options_conner);
		}

		return view;
	}

	class ViewHolder {

		TextView userNickName, content, createDate;
		ImageView user_icon;
	}
}
